package bearworker.zxl.service.impl;

import bearworker.zxl.dao.mapper.ManagerSuppInfoMapper;
import bearworker.zxl.dao.mapper.ManagerUserMapper;
import bearworker.zxl.entity.ManagerSuppInfo;
import bearworker.zxl.entity.ManagerUser;
import bearworker.zxl.service.UserService;
import bearworker.zxl.utils.CommUtil;
import bearworker.zxl.utils.CryptoUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 逍遥的熊 on 2017/11/27.
 */

@Service("user")
public class UserServiceImpl implements UserService {

    private Logger logger = Logger.getLogger(UserServiceImpl.class);

    Map<String, Object> resMap = new HashMap<>();

    @Autowired
    private ManagerSuppInfoMapper suppInfoMapper;

    @Autowired
    private ManagerUserMapper userMapper;

    public Map<String, Object> validUser(Map<String,String> params){


        try {
            String userName = params.get("username");
            String passwd = params.get("passwd");
            ManagerUser userEntity = userMapper.selectByUsername(userName);

            if (userEntity != null) {

                String psd = CryptoUtil.MD5(passwd + userEntity.getSalt());
                if (StringUtils.equals(userEntity.getPasswd(), psd)) {
                    userEntity.setPasswd("");
                    userEntity.setSalt("");
                    resMap = CommUtil.toResMap("0", "验证成功", userEntity);
                } else {
                    resMap = CommUtil.toResMap("-1", "用户验证失败，账号或密码错误", "false");
                }
            } else {
                resMap = CommUtil.toResMap("-2", "用户验证失败，账号或密码错误", "false");
            }
        }catch (Exception e){
            logger.error("/user/validUser错误",e);
            resMap = CommUtil.toResMap("-3", "系统错误", "false");
        }

        return resMap;
    }

    @Transactional
    public Map<String, Object> register(Map<String,String> params){
        try {
            String userName = params.get("username");
            String passwd = params.get("passwd");
            String suppName = params.get("suppName");

            int suppId = 0;
            ManagerUser userEntity = new ManagerUser();
            ManagerSuppInfo entity = suppInfoMapper.getSuppInfoByName(suppName);

            logger.debug(entity.getId() == null);

            if(entity.getId() == null){
                entity = new ManagerSuppInfo();
                entity.setSuppChargeRatio(1);
                entity.setSuppName(suppName);

                suppId = suppInfoMapper.insertSelective(entity);
            }else {
                suppId = entity.getId();
            }

            String salt = RandomStringUtils.randomAlphabetic(6);

            String psd = CryptoUtil.MD5(passwd+salt);

            userEntity.setUsername(userName);
            userEntity.setPasswd(psd);
            userEntity.setSalt(salt);
            userEntity.setSuppId(suppId);


            int res = userMapper.insertSelective(userEntity);


            resMap = CommUtil.toResMap("0", "注册成功", res);
        }catch (Exception e){
            logger.error("/user/insUser错误",e);
            resMap = CommUtil.toResMap("-3", "系统错误", "false");
        }

        return resMap;
    }
}
